layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        baseConfig = layui.baseConfig,
        table = layui.table;
    var util = layui.util;
    
    // 创建全局对象存储表格实例，确保子页面能够访问
    window.globalTableIns = null;
    
    // 获取URL参数函数
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
    
    // 检查是否需要刷新表格
    var needRefresh = getUrlParam("refresh");
    
    var tableIns = table.render({
        elem: '#list',
        url : "/registerInfo/list/",
        cellMinWidth : 95,
        height : "full-125",
        text: { none: ""},
        id : "listTable",
        cols : [[
            {field: 'id', title: "序号", align:"center", width: 100},
            {field: 'companyName', title: "公司名称(注册填入)", align:"center"},
            {field: 'license', title: "营业执照", align:"center"},
            {field: 'email', title: "邮箱", align:"center"},
            {field: 'bindCompanyName', title: "绑定公司名称", align:"center"},
            {field: 'createTime', title: "注册时间", align:"center", templet: function(d){
                return d.createTime ? util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss") : "";
            }},
            {title: '操作', templet: function(d){
                var html = '';
                if(d.companyId === null){
                    html += '<a class="layui-btn layui-btn-xs" lay-event="bindCompany">绑定公司</a>';
                }
                return html;
            }, align:'center', width: 120}
        ]],
        page: true,
        limits: [10, 15, 20, 25],
        limit: 10,
        response : {
            statusCode: 200 //规定成功的状态码，默认：0
        },
        parseData : function(res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.totalElements, //解析数据总数
                "data": res.data.content //解析数据列表
            };
        },
        done: function() {
            // 如果URL中有refresh参数，移除它以防止无限刷新
            if(needRefresh) {
                var currentUrl = window.location.href;
                var newUrl = currentUrl.replace(/[&?]refresh=\w+/, '');
                window.history.replaceState({}, document.title, newUrl);
            }
        }
    });
    
    // 保存表格实例到全局对象
    window.globalTableIns = tableIns;

    //列表操作
    table.on('tool(list)', function(obj){
        var data = obj.data;
        if(obj.event === 'bindCompany'){
            // 确保全局对象已设置
            window.globalTableObj = {
                tableIns: tableIns,
                layuiTable: table
            };
            
            console.log("打开弹窗前，设置全局表格对象");
            console.log(window.globalTableObj);
            
            layer.open({
                title: "绑定公司",
                type: 2,
                area: ["500px", "400px"],
                content: "/pages/registerInfo/bindCompany.html?id=" + data.id,
                success: function(layero, index){
                    console.log("弹窗加载完成");
                    // 尝试获取iframe窗口对象
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    console.log("iframe窗口对象获取状态:", !!iframeWin);
                }
            });
        }
    });
    
    // 添加全局刷新表格方法，供子窗口调用
    window.refreshTable = function() {
        console.log("调用全局刷新方法");
        if(window.globalTableIns) {
            window.globalTableIns.reload();
        } else {
            tableIns.reload();
        }
    };
});
